package hbase;

import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Client {

    public static void main(String[] args) throws IOException {

        String zkHosts = "47.101.216.12,47.101.206.249,47.101.204.23";
        String tableName = "rikun:student";
        // hbase 连接配置
        HbaseOption hbaseOption = new HbaseOption(zkHosts);
        // 建表
        hbaseOption.hbaseTableCreate(tableName);
        // 样例数据配置
        List<Put> data = new ArrayList<>();
        List<StudentInfo> simpleData = getSimpleDate();
        for (int i=0; i<5; i++) {
            Put put = new Put(Bytes.toBytes("0" + i));
            put.addColumn(Bytes.toBytes("name"), Bytes.toBytes(""), Bytes.toBytes(simpleData.get(i).getName()));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("student_id"), Bytes.toBytes(simpleData.get(i).getStudentId()));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("class"), Bytes.toBytes(simpleData.get(i).getClassId()));
            put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("understanding"), Bytes.toBytes(simpleData.get(i).getUnderstanding()));
            put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("programming"), Bytes.toBytes(simpleData.get(i).getProgramming()));
            data.add(put);
        }

        // 插入数据
        hbaseOption.putData(tableName, data);

        // hbase 查询条件
        List<Filter> conditions = new ArrayList<>();
        conditions.add(new SingleColumnValueFilter(Bytes.toBytes("info"),
                                                   Bytes.toBytes("student_id"),
                                                   CompareOperator.EQUAL,
                                                   Bytes.toBytes("20210000000001")));
        List<StudentInfo> rowData = hbaseOption.queryDataByFilter(tableName, conditions);
        int i = 1;
        for (StudentInfo info: rowData) {
            System.out.println("lineNo: " + i);
            System.out.println("name: " + info.getName());
            System.out.println("classId: " + info.getClassId());
            System.out.println("student_id: " + info.getStudentId());
            System.out.println("programming: " + info.getProgramming());
            System.out.println("understanding: " + info.getUnderstanding());
            i++;
        }

        // 删除数据
        hbaseOption.deleteDataByRowKey(tableName, "01");
    }

    /**
     * 样例数据
     *
     * @return List<StudentInfo>
     */
    private static List<StudentInfo> getSimpleDate() {

        List<StudentInfo> data = new ArrayList<>();

        StudentInfo info = new StudentInfo();
        info.setName("Tom");
        info.setStudentId("20210000000001");
        info.setClassId("1");
        info.setUnderstanding("75");
        info.setProgramming("82");
        data.add(info);

        StudentInfo info1 = new StudentInfo();
        info1.setName("Jerry");
        info1.setStudentId("20210000000002");
        info1.setClassId("2");
        info1.setUnderstanding("85");
        info1.setProgramming("67");
        data.add(info1);

        StudentInfo info2 = new StudentInfo();
        info2.setName("Jack");
        info2.setStudentId("20210000000003");
        info2.setClassId("2");
        info2.setUnderstanding("80");
        info2.setProgramming("80");
        data.add(info2);

        StudentInfo info3 = new StudentInfo();
        info3.setName("Rose");
        info3.setStudentId("20210000000004");
        info3.setClassId("2");
        info3.setUnderstanding("60");
        info3.setProgramming("61");
        data.add(info3);

        StudentInfo info4 = new StudentInfo();
        info4.setName("rikun");
        info4.setStudentId("G20200388010505");
        info4.setClassId("5");
        info4.setUnderstanding("98");
        info4.setProgramming("98");
        data.add(info4);

        return data;
    }



}
